﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using System.Xml;
using System.IO;

[assembly: WebResource("MyChart.Resource.FusionCharts.js", "application/javascript")]
namespace MyChart
{
    /// <summary>
    /// 所有图表控件的抽象基类
    /// </summary>
    public abstract class FusionChart : Control
    {
        #region 字段

        private readonly static XmlDocument _myChartConfig = new XmlDocument();
        
        private string _onClientClick = "";
        private string _toolTextFormat = "";

        private object _dataSource;

        /// <summary>
        /// 改成字符窜，支持%
        /// </summary>
        private string _width = "400";
        private string _height = "300";

        private string _caption = "";
        private string _subCaption = "";
        private bool _animation = true;
        private bool _isSliced = false;
        private ThemeType _theme = ThemeType.Black;
        private bool _showLabels = true;
        private bool _showValues = true;
        private string _numberPrefix = "";
        private string _numberSuffix = "";
        private int _decimals = 2;
        private int _marginTop = 5;
        private int _marginBottom = 2;
        private int _marginLeft = 2;
        private int _marginRight = 5;
        private bool _formatNumberScale = false;

        private Color _bgColor = Color.White;
        private bool _showBorder = false;

        private Font _baseFont = new Font("宋体", 12);
        private Color _foreColor = new Color();
        private bool _formatNumber = false;

        #endregion

        #region 属性

        /// <summary>
        /// 获取程序集加载的控件库全局配置文件
        /// </summary>
        protected static XmlDocument MyChartConfig
        {
            get { return FusionChart._myChartConfig; }
        } 

        /// <summary>
        /// 获取或设置客户端点击事件：FunctionName({Filed0},{Filed1},…… )
        /// </summary>
        public virtual string OnClientClick
        {
            get { return _onClientClick; }
            set { _onClientClick = value; }
        }

        /// <summary>
        /// 获取或设置数据项展现时弹出提示的格式化字符串
        /// 字段占位符格式：{字段名:格式符号}
        /// 
        /// 例：{Month:D2}月份销售总额：￥{TotalSaleMoney:N}
        /// 假设当前数据项对应数据源行的TotalSaleMoney字段值为250000，Month值为5
        /// 则显示为"05月份销售总额：￥250,000.00"
        /// </summary>
        public virtual string ToolTextFormat
        {
            get { return _toolTextFormat; }
            set { _toolTextFormat = value; }
        }

        /// <summary>
        /// 获取或设置控件的数据源
        /// </summary>
        public virtual object DataSource
        {
            get { return _dataSource; }
            set { _dataSource = value; }
        }

        /// <summary>
        /// 获取或设置控件的宽度（单位为像素或者%）
        /// </summary>
        public virtual string Width
        {
            get { return _width; }
            set { _width = value; }
        }

        /// <summary>
        /// 获取或设置控件的高度（单位为像素或者%）
        /// </summary>
        public virtual string Height
        {
            get { return _height; }
            set { _height = value; }
        }

        /// <summary>
        /// 获取或设置控件的标题文本
        /// </summary>
        public virtual string Caption
        {
            get { return _caption; }
            set { _caption = value; }
        }

        /// <summary>
        /// 获取或设置控件的副标题文本
        /// </summary>
        public virtual string SubCaption
        {
            get { return _subCaption; }
            set { _subCaption = value; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否启用动画
        /// </summary>
        public virtual bool Animation
        {
            get { return _animation; }
            set { _animation = value; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否启静态展现
        /// </summary>
        public virtual bool IsSliced
        {
            get { return _isSliced; }
            set { _isSliced = value; }
        }

        /// <summary>
        /// 获取或设置图表的主题类型
        /// </summary>
        public virtual ThemeType Theme
        {
            get { return _theme; }
            set { _theme = value; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否显示标签文本
        /// </summary>
        public virtual bool ShowLabels
        {
            get { return _showLabels; }
            set { _showLabels = value; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否显示数据项的值
        /// </summary>
        public virtual bool ShowValues
        {
            get { return _showValues; }
            set { _showValues = value; }
        }

        /// <summary>
        /// 获取或设置展现数据项值的前缀文本
        /// 例：￥250,000.00
        /// </summary>
        public virtual string NumberPrefix
        {
            get { return _numberPrefix; }
            set { _numberPrefix = value; }
        }

        /// <summary>
        /// 获取或设置展现数据项值的后缀文本
        /// 例：25%
        /// </summary>
        public virtual string NumberSuffix
        {
            get { return _numberSuffix; }
            set { _numberSuffix = value; }
        }

        /// <summary>
        /// 获取或设置数据项展示时值的四舍五入精度
        /// </summary>
        public virtual int Decimals
        {
            get { return _decimals; }
            set { _decimals = value; }
        }

        /// <summary>
        /// 获取或设置图表与flash区域的上边距
        /// </summary>
        public virtual int MarginTop
        {
            get { return _marginTop; }
            set { _marginTop = value; }
        }

        /// <summary>
        /// 获取或设置图表与flash区域的下边距
        /// </summary>
        public virtual int MarginBottom
        {
            get { return _marginBottom; }
            set { _marginBottom = value; }
        }

        /// <summary>
        /// 获取或设置图表与flash区域的左边距
        /// </summary>
        public virtual int MarginLeft
        {
            get { return _marginLeft; }
            set { _marginLeft = value; }
        }

        /// <summary>
        /// 获取或设置图表与flash区域的右边距
        /// </summary>
        public virtual int MarginRight
        {
            get { return _marginRight; }
            set { _marginRight = value; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否启用数量级缩写
        /// </summary>
        public bool FormatNumberScale
        {
            get { return _formatNumberScale; }
            set { _formatNumberScale = value; }
        }

        /// <summary>
        /// 获取或设置flash区域的背景色
        /// </summary>
        public virtual Color BgColor
        {
            get { return _bgColor; }
            set { _bgColor = value; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否显示flash区域的边框
        /// </summary>
        public virtual bool ShowBorder
        {
            get { return _showBorder; }
            set { _showBorder = value; }
        }

        /// <summary>
        /// 获取或设置控件的文本字体
        /// </summary>
        public virtual Font BaseFont
        {
            get { return _baseFont; }
            set { _baseFont = value; }
        }

        /// <summary>
        /// 获取或设置控件的前景色
        /// </summary>
        public virtual Color ForeColor
        {
            get { return _foreColor; }
            set { _foreColor = value; }
        }
        /// <summary>
        /// 获取或设置数字是否存在千分位
        /// </summary>
        public virtual bool FormatNumber
        {
            get { return _formatNumber; }
            set { _formatNumber = value; }
        }

        /// <summary>
        /// 设置或获取是否在鼠标提示中显示百分比(目前只在Pie2D图表中实现)
        /// </summary>
        public virtual bool ShowPercentInToolTip { get; set; }

        /// <summary>
        /// 设置或获取一个值，是否控制图表的绘制，如果设置为True则需要自己去调用chart_{ChartID}.render("{ChartID}Div"),FusionCharts提供了一个FC_Rendered(ChartId)事件
        /// </summary>
        public bool IsCustomRender { get; set; }
        #endregion

        #region 构造器

        /// <summary>
        /// 静态构造器，加载程序集时导入控件库配置文件
        /// </summary>
        static FusionChart()
        {
            _myChartConfig.Load(AppDomain.CurrentDomain.BaseDirectory + "\\PubProject\\bin\\MyChart.config");
        }

        #endregion

        #region 抽象方法

        /// <summary>
        /// 要求子类实现数据绑定功能
        /// </summary>
        new public abstract void DataBind();

        /// <summary>
        /// 要求子类实现根据当前属性获取不同的flash文件相对路径
        /// </summary>
        /// <returns>根据当前属性获取不同的flash文件相对路径</returns>
        protected abstract string GetFlashPath();

        #endregion

        #region 方法

        /// <summary>
        /// 格式化输出的html代码（比如替换%为%25）
        /// </summary>
        /// <param name="html">格式化前的html代码</param>
        /// <returns>格式化后的html代码</returns>
        public virtual string FormatRenderHtml(string html)
        {
            return html.Replace("%", "%25");
        }

        #endregion
    }
}
